import { User, CrawlerTask, CrawledData } from '@weubi/shared';
import { faker } from '@faker-js/faker';

/**
 * 测试数据工厂类
 * 基于奥卡姆剃刀原则：提供简洁的测试数据生成方法
 */
export class TestDataFactory {
  
  // 生成用户测试数据
  static createUserData(overrides: Partial<User> = {}): Partial<User> {
    return {
      email: faker.internet.email(),
      name: faker.person.fullName(),
      password: 'test123',
      role: 'user' as const,
      isActive: true,
      emailVerified: true,
      loginCount: 0,
      ...overrides
    };
  }

  // 生成管理员测试数据
  static createAdminData(overrides: Partial<User> = {}): Partial<User> {
    return this.createUserData({
      role: 'admin' as const,
      ...overrides
    });
  }

  // 生成爬虫任务测试数据
  static createTaskData(overrides: Partial<CrawlerTask> = {}): Partial<CrawlerTask> {
    return {
      name: faker.lorem.words(3),
      description: faker.lorem.sentence(),
      url: faker.internet.url(),
      config: {
        selectors: {
          title: 'h1',
          content: '.content',
          author: '.author'
        },
        delay: 1000,
        retries: 3,
        userAgent: 'TestBot/1.0'
      },
      status: 'pending' as const,
      engine: 'http' as const,
      isActive: true,
      priority: faker.helpers.arrayElement(['low', 'normal', 'high', 'urgent'] as const),
      ...overrides
    };
  }

  // 生成爬取数据测试数据
  static createCrawledData(overrides: Partial<CrawledData> = {}): Partial<CrawledData> {
    const title = faker.lorem.sentence();
    const content = faker.lorem.paragraphs(3);
    const url = faker.internet.url();

    return {
      url,
      data: {
        title,
        content,
        ...overrides.data
      },
      hash: CrawledData.generateHash({ title, content }, url),
      engine: 'http' as const,
      isValid: true,
      isProcessed: false,
      metadata: {
        crawledAt: new Date().toISOString(),
        responseTime: faker.number.int({ min: 100, max: 5000 }),
        statusCode: 200
      },
      ...overrides
    };
  }

  // 生成登录请求数据
  static createLoginData(overrides: any = {}) {
    return {
      email: 'test@weubi.com',
      password: 'test123',
      ...overrides
    };
  }

  // 生成注册请求数据
  static createRegisterData(overrides: any = {}) {
    return {
      email: faker.internet.email(),
      name: faker.person.fullName(),
      password: 'password123',
      ...overrides
    };
  }

  // 生成无效的邮箱格式
  static createInvalidEmails(): string[] {
    return [
      'invalid-email',
      '@domain.com',
      'user@',
      'user.domain.com',
      'user@domain',
      ''
    ];
  }

  // 生成无效的密码
  static createInvalidPasswords(): string[] {
    return [
      '', // 空密码
      '123', // 太短
      'a', // 太短
      'ab', // 太短
    ];
  }

  // 生成批量测试数据
  static createBulkData<T>(
    factory: () => T,
    count: number
  ): T[] {
    return Array.from({ length: count }, () => factory());
  }

  // 生成随机字符串
  static randomString(length: number = 10): string {
    return faker.string.alphanumeric(length);
  }

  // 生成随机数字
  static randomNumber(min: number = 1, max: number = 100): number {
    return faker.number.int({ min, max });
  }

  // 生成随机布尔值
  static randomBoolean(): boolean {
    return faker.datatype.boolean();
  }

  // 生成随机日期
  static randomDate(start?: Date, end?: Date): Date {
    return faker.date.between({
      from: start || new Date('2023-01-01'),
      to: end || new Date()
    });
  }
}
